package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper ;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        //判断是否从前台传来序列,没有则自动添加
        if (strategyDetail.getSequence() == null) {
            int max = strategyDetailMapper.selectMaxSequence(strategyDetail.getStrategyCatalog().getId());
            strategyDetail.setSequence(max+1);
        }
        StrategyContent strategyContent = strategyDetail.getStrategyContent();//获取文章内容对象
        if (strategyDetail.getId() != null) {
            //修改
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //修改文章内容
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }else{
            //新增
            strategyDetailMapper.insert(strategyDetail);
            //修改文章内容
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }
    }

    @Override
    public List listAll() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail getDetailById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }
}
